package com.res.share.controller;

import com.res.share.common.Constants;
import com.res.share.model.ResArt;
import com.res.share.model.ResUser;
import com.res.share.model.dto.ArticleModel;
import com.res.share.model.dto.UserStatisModel;
import com.res.share.service.IResArtService;
import com.res.share.service.IResUserService;
import com.res.share.service.PersonService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping("/person")
public class PersonController extends BaseController {

    @Autowired
    private PersonService personService;

    @Autowired
    private IResUserService resUserService;


    @Autowired
    private IResArtService resArtService;

    /**
     * 个人中心
     * @param model
     * @return
     */
    @GetMapping(value = {"/info.html"})
    public String info(Model model, HttpServletRequest request) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/info.html";
        }
        UserStatisModel userStatisModel=personService.findPersionDetail(userId);
        model.addAttribute("show",0);
        model.addAttribute("userStatisModel",userStatisModel);
        return "person-pc/person";
    }

    /**
     * 我的信息
     * @param model
     * @param request
     * @param response
     * @return
     */
    @GetMapping(value = {"/userinfo.html"})
    public String userinfo(Model model, HttpServletRequest request, HttpServletResponse response) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/userinfo.html";
        }
        ResUser resUser=personService.findCurrentUserInfo(userId);
        model.addAttribute("resUser",resUser);
        model.addAttribute("show",1);
        return "person-pc/userinfo";
    }

    /**
     * 足迹
     * @param model
     * @param request
     * @param pageNum
     * @return
     */
    @GetMapping(value = {"/resHistory.html"})
    public String findHistoryList(Model model, HttpServletRequest request,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/resHistory.html";
        }
        Map<String,Object> resultMap=personService.findHistoryList(userId,pageNum);
        model.addAttribute("show",4);
        model.addAttribute("requestUrl","/person/resHistory.html");
        model.addAllAttributes(resultMap);
        return "person-pc/history";
    }

    /**
     * 我的收藏
     * @param model
     * @param request
     * @param pageNum
     * @return
     */
    @GetMapping(value = {"/collection.html"})
    public String collection(Model model, HttpServletRequest request,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/collection.html";
        }
        Map<String,Object> resultMap=personService.findCollectionAricle(userId,pageNum);
        model.addAttribute("show",2);
        model.addAttribute("requestUrl","/person/collection.html");
        model.addAllAttributes(resultMap);
        return "person-pc/collection";
    }

    /**
     * 财务明细
     * @param model
     * @param request
     * @param pageNum
     * @return
     */
    @GetMapping(value = {"/mymoney.html"})
    public String findMyjifenList(Model model, HttpServletRequest request,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/mymoney.html";
        }
        Map<String,Object> resultMap=personService.findMyjifenList(userId,pageNum);
        model.addAttribute("requestUrl","/person/mymoney.html");
        model.addAllAttributes(resultMap);
        model.addAttribute("show",3);
        return "person-pc/myscoreandorder";
    }

    /**
     * 更新邮箱
     * @param model
     * @param request
     * @return
     */
    @GetMapping(value = {"/editEmail.html"})
    public String editEmail(Model model, HttpServletRequest request) {
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute == null || StringUtils.isEmpty(attribute.toString())) {
            return "redirect:/to/login.html?redirectUrl=/person/editEmail.html";
        }
        return "person-pc/editEmail";
    }


    /**
     * 我的下载
     * @param model
     * @param request
     * @param pageNum
     * @return
     */
    @GetMapping(value = {"/downres.html"})
    public String downres(Model model, HttpServletRequest request,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/downres.html";
        }
        Map<String,Object> resultMap=personService.findResDownList(userId,pageNum);
        model.addAttribute("requestUrl","/person/downres.html");
        model.addAllAttributes(resultMap);
        model.addAttribute("show",0);
        return "person-pc/downres";
    }

    /**
     * 开通会员
     * @param model
     * @return
     */
    @GetMapping(value = {"/govip.html"})
    public String govip(Model model,@RequestParam(value = "redirectUrl",required = false) String redirectUrl) {
        model.addAttribute("show",0);
        model.addAttribute("redirectUrl", redirectUrl);
        return "person-pc/govip";
    }

    /**
     * 会员
     * @param request
     * @return
     */
    @GetMapping("/goMmeberVip.html")
    public String goVip(HttpServletRequest request) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/goMmeberVip.html";
        }
        return "redirect:/person/govip.html";
    }


    /**
     *
     * 投稿列表
     * @param model
     * @param request
     * @param pageNum
     * @return
     */
    @GetMapping(value = {"/reports.html"})
    public String reports(Model model, HttpServletRequest request, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/reports.html";
        }
        Map<String,Object> resultMap=personService.findMyReports(userId,pageNum);
        model.addAttribute("requestUrl","/person/reports.html");
        model.addAllAttributes(resultMap);
        model.addAttribute("show",5);
        return "person-pc/reports";
    }


    /**
     * 关注我的
     * @param model
     * @param request
     * @param pageNum
     * @return
     */
    @GetMapping(value = {"/likeme.html"})
    public String likemeDetail(Model model, HttpServletRequest request, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/likeme.html";
        }
        Map<String,Object> resultMap=personService.likemeDetailList(userId,pageNum);
        model.addAttribute("requestUrl","/person/likeme.html");
        model.addAllAttributes(resultMap);
        model.addAttribute("show",12);
        return "person-pc/likeme";
    }


    /**
     * 我关注的
     * @param model
     * @param request
     * @param pageNum
     * @return
     */
    @GetMapping(value = {"/mylikeuser.html"})
    public String mylikeuser(Model model, HttpServletRequest request, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/mylikeuser.html";
        }
        Map<String,Object> resultMap=personService.mylikeuserList(userId,pageNum);
        model.addAttribute("requestUrl","/person/mylikeuser.html");
        model.addAllAttributes(resultMap);
        model.addAttribute("show",11);
        return "person-pc/mylikeuser";
    }


    /**
     * TA的动态
     * @param model
     * @param request
     * @param pageNum
     * @return
     */
    @GetMapping(value = {"/mylikeuserres.html"})
    public String mylikeuserres(Model model, HttpServletRequest request, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/mylikeuserres.html";
        }
        Map<String,Object> resultMap=personService.mylikeuserresList(userId,pageNum);
        model.addAttribute("requestUrl","/person/mylikeuserres.html");
        model.addAllAttributes(resultMap);
        model.addAttribute("show",13);
        return "person-pc/mylikeuserres";
    }


    /**
     * 投稿明细
     * @param model
     * @param request
     * @param response
     * @return
     */
    @GetMapping(value = {"/reportDetail.html"})
    public String reportDetail(Model model, HttpServletRequest request, HttpServletResponse response) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/reportDetail.html";
        }
        String articleId = request.getParameter("articleId");
        //分类
        //文章详情
        ResArt articleModel=null;
        if(StringUtils.isEmpty(articleId)){
            articleModel = resArtService.findArtDataById(Integer.valueOf(articleId));
        }
        model.addAttribute("articleModel",articleModel);
        model.addAttribute("requestUrl","/person/reportDetail.html");
        model.addAttribute("show",6);
        return "person-pc/reportDetail";
    }


    /**
     * @param model
     * @param request
     * @param response
     * @return
     */
    @GetMapping(value = {"/userpayrecharge.html"})
    public String payaccountrecharge(Model model, HttpServletRequest request, HttpServletResponse response) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/userpayrecharge.html";
        }
        //查询用户
        Integer nums=personService.findUserMoney(userId);
        model.addAttribute("nums",nums==null?0:nums);
        model.addAttribute("show",0);
        return "person-pc/pay_account_recharge";
    }


    /**
     * 发送消息页面
     * @param model
     * @param request
     * @return
     */
    @GetMapping(value = {"/sendMessage/{receiveId}.html"})
    public String sendMessage(Model model, HttpServletRequest request,
                              @PathVariable(value = "receiveId") String receiveId) {
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute == null || StringUtils.isEmpty(attribute.toString())) {
            return "redirect:/to/login.html?redirectUrl=/person/sendMessage/"+receiveId+".html";
        }
        if(receiveId!=null){
            ResUser resUser = resUserService.getById(receiveId);
            model.addAttribute("receiveUserName",resUser.getUserName());
        }else {
            model.addAttribute("receiveUserName",null);
        }
        model.addAttribute("receiveId",receiveId);
        model.addAttribute("show",7);
        return "person-pc/sendmessage";
    }

    @GetMapping(value = {"/sendMessage.html"})
    public String sendMessageOne(Model model, HttpServletRequest request) {
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute == null || StringUtils.isEmpty(attribute.toString())) {
            return "redirect:/to/login.html?redirectUrl=/person/sendMessage.html";
        }
        model.addAttribute("receiveUserName",null);
        model.addAttribute("receiveId",null);
        model.addAttribute("show",7);
        return "person-pc/sendmessage";
    }



    //发件箱
    @GetMapping(value = {"/findSendBox.html"})
    public String findSendBox(Model model, HttpServletRequest request,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/findSendBox.html";
        }
        Map<String,Object> resultMap=personService.findSendBoxList(userId,pageNum);
        model.addAttribute("show",8);
        model.addAttribute("requestUrl","/person/findSendBox.html");
        model.addAllAttributes(resultMap);
        return "person-pc/sendBox";
    }


    //收件箱
    @GetMapping(value = {"/receiveBox.html"})
    public String findReceiveBox(Model model, HttpServletRequest request,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/receiveBox.html";
        }
        Map<String,Object> resultMap=personService.findReceiveBoxList(userId,pageNum);
        model.addAttribute("show",9);
        model.addAttribute("requestUrl","/person/receiveBox.html");
        model.addAllAttributes(resultMap);
        return "person-pc/receiveBox";
    }

    //系统消息
    @GetMapping(value = {"/systemMessage.html"})
    public String findSystemMessage(Model model, HttpServletRequest request,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Integer userId = null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId =Integer.valueOf(attribute.toString());
        }else {
            return "redirect:/to/login.html?redirectUrl=/person/systemMessage.html";
        }
        Map<String,Object> resultMap=personService.findSystemMessageList(userId,pageNum);
        model.addAttribute("show",10);
        model.addAttribute("requestUrl","/person/systemMessage.html");
        model.addAllAttributes(resultMap);
        return "person-pc/systemMessage";
    }

}
